package com.yang.demo.greendao;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.yang.demo.Model;
import com.yang.demo.ProjectApplication;
import com.yang.demo.greendao.gen.DaoMaster;
import com.yang.demo.greendao.gen.DaoSession;
import com.yang.demo.greendao.gen.ModelDao;
import com.yang.demo.greendao.gen.UserModelDao;
import com.yang.demo.view.UserModel;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;


/**
 * @author yangwenwen
 * @date 2018/1/2
 * @describe
 */

public class DaoManager {

    private static final String DB_NAME = "mould.db";


    private static final class Manager {
        private static final DaoManager mDaoManager = new DaoManager();

    }

    private static final class DaoInstance {
        private static final DaoMaster.DevOpenHelper mHelper =
                new DaoMaster.DevOpenHelper(ProjectApplication.getApplication(), DB_NAME, null);
        private static final SQLiteDatabase db = mHelper.getWritableDatabase();
        private static final DaoMaster mDaoMaster = new DaoMaster(db);
        private static final DaoSession mDaoSession = mDaoMaster.newSession();
    }

    public static DaoManager getInstance() {
        return Manager.mDaoManager;
    }


    private DaoSession getDaoSession() {
        return DaoInstance.mDaoSession;
    }


    public void setUserModel(Model userBean) {
        getDaoSession().getModelDao().detachAll();
        getDaoSession().getModelDao().insertOrReplace(userBean);
    }



    public void insertData(Model model) {
        ModelDao userDao = getDaoSession().getModelDao();
        userDao.insert(model);
    }

    public List<Model> getDataModel() {
        return getDaoSession().queryBuilder(Model.class).build().list();
    }


    public List<Model> queryDataList() {
        ModelDao modelDao = getDaoSession().getModelDao();
        QueryBuilder<Model> qb = modelDao.queryBuilder();
        List<Model> list = qb.list();
        return list;
    }


    public void addUser(UserModel model) {
        UserModelDao modelDao = getDaoSession().getUserModelDao();
        modelDao.insert(model);
    }

    public List<UserModel> getUserList(){
        UserModelDao modelDao = getDaoSession().getUserModelDao();
        QueryBuilder<UserModel> qb = modelDao.queryBuilder();
        List<UserModel> list = qb.list();
        return list;
    }





}
